package com.csii.mobilebanking;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.csii.mobilebanking.Strings.Extra;

public class LoadURLWebView extends Activity implements OnClickListener{
	
	private WebView webView;
	private TextView titleText;
	private ProgressBar progressBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.load_url_webview);
		loadView();
		WebSettings settings=webView.getSettings();
		settings.setJavaScriptEnabled(true);
		settings.setBuiltInZoomControls(true);
		titleText.setText(getIntent().getStringExtra(Extra.WEB_VIEW_TITLE));
		webView.setWebViewClient(new webViewClient());
		webView.setWebChromeClient(new WebChromeClient() {  
	        //当WebView进度改变时更新窗口进度  
	        @Override  
	        public void onProgressChanged(WebView view, int newProgress) {
	        	progressBar.setProgress(newProgress);
	        	if(newProgress==100){
	        		progressBar.setVisibility(View.GONE);
	        	}
	        }  
	    });
		webView.loadUrl(getIntent().getStringExtra(Extra.WEB_VIEW_URL));
		
	}

	private void loadView() {
		webView=(WebView)findViewById(R.id.webview);
		progressBar=(ProgressBar)findViewById(R.id.progressbar);
		((Button)findViewById(R.id.title_bak)).setOnClickListener(this);
		titleText=(TextView)findViewById(R.id.content_title);
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.title_bak:
			finish();
			break;
		}
		
	}

	@Override
	public void onBackPressed() {
		if(webView.canGoBack()){
			progressBar.setVisibility(View.VISIBLE);
			webView.goBack();
		}else{
			finish();
		}
	}
	class webViewClient extends WebViewClient{
		
	    //重写shouldOverrideUrlLoading方法，使点击链接后不使用其他的浏览器打开。  
	    @Override  
	    public boolean shouldOverrideUrlLoading(WebView view, String url) {
	    	progressBar.setProgress(0);
	    	progressBar.setVisibility(View.VISIBLE);
	        view.loadUrl(url);
	        //如果不需要其他对点击链接事件的处理返回true，否则返回false  
	        return true;  
	    }  
	    
	   }
}
